package com.garmin.tw.garminpcsync.android;

import java.net.URI;

import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCMethod;
import org.xmlrpc.android.XMLRPCMethodCallback;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.garmin.tw.garminpcsync.android.adapter.RichPoiAdapter;
import com.garmin.tw.garminpcsync.common.domain.xmlrpc.GPIResult;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Tracker;

public class RichPoiListViewActivity extends ListActivity {

	private URI uri;
	
	private XMLRPCClient client;
	
	private ProgressDialog progressDialog;
	
	//private ListView mainListView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.uri = URI.create(getString(R.string.server_url));
		
		this.client = new XMLRPCClient(uri);
		
		XMLRPCMethod method = new XMLRPCMethod(client, "APIDispatcher.getGPIList",
			new XMLRPCMethodCallback() {
				public void callFinished(Object result) {
					if (result instanceof GPIResult) {
						GPIResult gpiResult = (GPIResult)result;
						
						if (gpiResult.getGpiList() != null) {
														
							// Create ArrayAdapter using the planet list.
							//listAdapter = new ArrayAdapter<String>(RichPoiListViewActivity.this, R.layout.simplerow, titleList);
							RichPoiAdapter listAdapter = new RichPoiAdapter(RichPoiListViewActivity.this, gpiResult.getGpiList());

							// Set the ArrayAdapter as the ListView's adapter.
							ListView mainListView = getListView();
							mainListView.setAdapter(listAdapter);
							mainListView.setOnItemClickListener(new OnItemClickListener() {
								@Override
								public void onItemClick(AdapterView<?> parent, View view,
										int position, long id) {
									// Get current context.
									Context mCtx = RichPoiListViewActivity.this;
									// setContext will use mCtx to retrieve the application context.
									EasyTracker.getInstance().setContext(mCtx);
									// EasyTracker is now ready for use.
									Tracker myTracker = EasyTracker.getTracker();
									
									TextView title = (TextView) view.findViewById(R.id.description);
									
									myTracker.trackEvent("RichPoi List Page", "Download RichPoi", title.getText().toString(), null);
									
									// When clicked, show a toast with the TextView text
									Toast.makeText(getApplicationContext(), title.getText(), Toast.LENGTH_SHORT).show();
								}
							});
	
						}
					}
					
					progressDialog.cancel();
				}
			});
		
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage("Loading. Please wait...");
		progressDialog.show();
		
		Object[] params = { "3371570511", "zh_TW" };
		method.call(params);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		// ... // The rest of your onStart() code.
		EasyTracker.getInstance().activityStart(this); // Add this method.
	}

	@Override
	public void onStop() {
		super.onStop();
		// ... // The rest of your onStop() code.
		EasyTracker.getInstance().activityStop(this); // Add this method.
	}
}